<template>
    <div>
        <van-pull-refresh v-model="tabRefreshing" @refresh="tableViewRefresh">

            <van-list v-model="tableViewLoading" :finished="tableViewFinished" :finished-text='requestFinishedMessage'
                :error-text="requestErrorMessage" @load='tableViewLoadMore' :immediate-check="false"
                style="margin-bottom: 50px;">

                <van-cell v-for="(item, index) in tableViewDataList" :key="index" @click="cellDidSelectMethod(item)">
                    <div>

                        <div class="titlefont"> {{ '【' + item.projectID + '】' + item.projectName }}</div>
                        <div class="infosfont"> 项目交接发起时间: {{ item.startTime }}</div>
                        <div class="infosfont" style="display:flex;justify-content:space-between">
                            <div> 项目类型: {{ item.competitiveMode }} </div>

                            <van-button v-if="showCellButton(item)"
                                style=" display: inline-block; margin:0 9px 0 9px; padding:0 9px 0 9px"
                                @click="cellButtonClick_Two(item)" size="small" type="primary"
                                color="#60a9ec">【交付提醒】</van-button>

                            <van-button style=" display: inline-block; margin:0 9px 0 9px; padding:0 9px 0 9px"
                                @click="cellButtonClick(item)" size="small" type="primary"
                                color="#60a9ec">【发起交接】</van-button>
                        </div>
                    </div>
                </van-cell>

                <!-- 请求第一页数据时，出现错误或无数据 展示空白提示界面
                请求后续数据时，只做弹窗提示 -->
                <div v-show="showEmptyView" class="text-c">
                    <img width="100px" height="100px" :src="require('@/assets/images/pic_nodata.png')"
                        style="margin: 120px 50px 0" @click="tableViewRefresh()" />
                    <div class="color-b font14" style="margin: 20px 0 0 15px">
                        暂无数据
                    </div>
                </div>

                <div v-show="showErrorView" class="text-c">
                    <img width="100px" height="100px" :src="require('@/assets/images/pic_neterror.png')" alt="获取数据失败"
                        style="margin: 120px 50px 0" @click="tableViewRefresh()" />
                    <div class="color-b font14" style="margin: 20px 0 0 15px">
                        {{ requestErrorMessage }}
                    </div>
                </div>

            </van-list>
        </van-pull-refresh>
    </div>
</template>

<script>

import { requestDrawupProjectListData, processVerificationByTableAndParameters, findByBusinessIdAndVerify } from "@/api/dictapi";
import jDrawupUtil, { dict_xmjj_code } from "../../assets/js/dict_process_helper";
import jTool from "@/assets/js/jHelper";

import { Toast, Dialog } from "vant";
import { Notify } from "vant";
import { mapGetters } from "vuex";
import moment from 'moment'

export default {
    name: 'sjlistview',
    data() {
        return {

            requestParam: {
                page: 1, rows: 10, size: 10,
                projectName: '', // 项目名称
                projectID: '',  // 项目ID
            },

            requestFinishedMessage: '',
            requestErrorMessage: '',

            tableViewDataList: [],
            tabRefreshing: false,
            tableViewLoading: false,
            tableViewFinished: false,
        }
    },

    props: ['pushParams'],

    computed: {
        ...mapGetters(["user"]),
        ...mapGetters(["userinfo"]),

        showEmptyView() {

            return this.requestParam.page === 1 && this.tableViewDataList.length === 0 && jTool.stringIsEmpty(this.requestErrorMessage);
        },

        showErrorView() {

            return this.requestParam.page === 1 && !jTool.stringIsEmpty(this.requestErrorMessage);
        },


    },

    created() {

        this.tableViewDataList = [];
        this.tableViewRefresh()
    },

    mounted() {

    },

    activated() {


    },

    methods: {

        // 查询
        requestDataWithFliterCondition(params) {

            Object.assign(this.requestParam, params);
            this.tableViewRefresh();
        },

        tableViewRefresh() {

            this.requestParam.page = 1;

            this.tabRefreshing = true;

            this.tableViewLoading = true;
            this.tableViewFinished = false;
            this.requestListDatas();
        },

        tableViewLoadMore() {

            if (!jTool.stringIsEmpty(this.requestFinishedMessage)) {
                this.tableViewLoading = false
                this.tableViewFinished = true;
            }
            else {

                this.tabRefreshing = false;
                this.requestListDatas();
            }
        },

        requestListDatas() {

            let urlChip = jDrawupUtil.getSJListURLWithCode(this.pushParams.processID);
            requestDrawupProjectListData(urlChip, this.requestParam)
                .then((result) => {

                    if (!result.data) {
                        this.tableViewFinished = true;
                        this.requestCompletion('result.message');
                        return;
                    }

                    let listdetails = [];
                    if (result.data.data.content) {
                        listdetails = listdetails.concat(result.data.data.content);
                    }

                    if (this.requestParam.page == 1) {
                        this.tableViewDataList = listdetails;
                    }
                    else {
                        this.tableViewDataList = this.tableViewDataList.concat(listdetails);
                    }

                    if (result.data.data.totalPages <= this.requestParam.page) {
                        this.requestFinishedMessage = '没有更多数据！';
                        this.tableViewFinished = true;
                    }
                    else {
                        this.requestParam.page++;
                        this.requestFinishedMessage = '';
                    }

                    this.requestCompletion('');
                })
                .catch((error) => {
                    this.tableViewFinished = true;
                    this.requestCompletion('获取数据失败,请稍后重试');
                });
        },


        requestCompletion(error) {

            this.tabRefreshing = false;
            this.tableViewLoading = false

            if (!jTool.stringIsEmpty(error)) {
                this.requestErrorMessage = error;
                Notify({ type: 'warning', message: error })
            }
        },

        timeUtil(value) {
            return moment(value).format('YYYY-MM-DD HH:mm:ss');
        },

        // 
        showCellButton(item) {

            return item.projectHandoverStatus == '售前交接申请已接收'
        },


        // 验收到期提醒推送
        cellButtonClick_Two(item) {

            let params = {
                isDrawup: '1',
                pushType:'push',
                formData: {
                    deliverId: item.deliverId,
                    projectID: item.projectID,
                },
            }

            this.$router.push(
                {
                    name: 'xmjj_dqtx_push',
                    query: params,
                }
            )
        },

        // 发起交接
        // 直签: 校验合同资料交底 - 获取签约效益评估
        // 非直签: 校验项目资料交底 - 校验合同资料交底 - 获取签约效益评估
        cellButtonClick(item) {

            if (!(jTool.stringIsEmpty(item.projectHandoverStatus) || item.projectHandoverStatus == '售前交接申请已退回')) {
                Dialog.alert({
                    title: '温馨提示', message: '已发起项目交接，不可重复发起!'
                });
                return;
            }

            let competitiveMode = item.competitiveMode;
            if (competitiveMode == '直签') {
                this.ZQ_Drawup_XMJJ(item);
            }
            else {
                this.Other_Drawup_XMJJ(item);
            }
        },


        // 非直签: 校验项目资料交底（== true）- 校验合同资料交底（== true && projectKind == "投资"） - 可以起草
        // 非直签: 校验项目资料交底（== true）- 校验合同资料交底（== true && projectKind != "投资"）- 获取签约效益评估（== true || projectMode == "纯受托代销(税率仅为13%)"） - 可以起草
        Other_Drawup_XMJJ(item) {
            
            Toast.loading({ message: "加载中", forbidClick: false });
            processVerificationByTableAndParameters(item.projectID, 'us_project_disclosure')
                .then((result) => {

                    Toast.clear();
                    let flag = result.data.data;
                    if (!flag) {

                        let message = '该项目无项目资料交底信息或未归档，无法发起项目交接';
                        Dialog.alert({ title: '温馨提示', message: message });
                    }
                    else {

                        this.ZQ_Drawup_XMJJ(item);
                    }
                })
                .catch((error) => {
                    Toast.clear();
                    Notify({ type: 'warning', message: '起草请求失败,请稍后重试' })
                });
        },



        // 直签: 校验合同资料交底（== true && projectKind == "投资"） - 可以起草 
        // 直签: 校验合同资料交底（== true && projectKind != "投资"）- 获取签约效益评估（== true || projectMode == "纯受托代销(税率仅为13%)"） - 可以起草
        ZQ_Drawup_XMJJ(item) {

            let projectKind = item.projectKind;
            let projectMode = item.projectMode;
            Toast.loading({ message: "加载中", forbidClick: false });
            processVerificationByTableAndParameters(item.projectID, 'us_contract_disclose')
                .then((result) => {

                    Toast.clear();
                    let flag = result.data.data;
                    if (!flag) {

                        let message = '该项目无合同资料交底信息或未归档，无法发起项目交接';
                        Dialog.alert({ title: '温馨提示', message: message });
                    }
                    else {
                        if (projectKind != "投资") {
                            findByBusinessIdAndVerify(item.projectID)
                                .then((result) => {
                                    Toast.clear();
                                    let dataDict = result.data.data;
                                    if (dataDict =='' || projectMode == "纯受托代销(税率仅为13%)"){
                                        this.pushDrawupDetail(item);
                                    }
                                    else {
                                        let message = '请完成合同计划中签约效益评估，并且IT部分毛利率 大于0小于100！，才可发起项目交接"';
                                        Dialog.alert({ title: '温馨提示', message: message });
                                    }
                                })
                                .catch((error) => {
                                    Toast.clear();
                                    Notify({ type: 'warning', message: '起草请求失败,请稍后重试' })
                                });
                        }
                        else {
                            this.pushDrawupDetail(item);
                        }
                    }
                })
                .catch((error) => {
                    Toast.clear();
                    Notify({ type: 'warning', message: '起草请求失败,请稍后重试' })
                });
        },
        // 可以起草
        pushDrawupDetail(item) {

            let pushParams = this.pushParams;
            pushParams.projectInformationId = item.projectInformationId;
            this.$router.push(
                {
                    name: 'xmjj',
                    query: pushParams
                }
            )
        },
        cellDidSelectMethod(value) {

        }
    }
}
</script>

<style scoped></style>